<?php
namespace app\controller;

use app\BaseController;
use think\facade\Request;
use think\facade\Db;
use think\Exception;


class Customer extends BaseController
{
    //获取公司所有客户信息
    public function Get_CustomerInfo()
    {
        $userId = $this->request->auth['data']->user->user_id;
        $current_page = Request::param('current_page');
        $page_size = Request::param('page_size');

        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        $customerInfo = Db::name('customer')
            ->where('customer_company_id', $companyId)
            ->page($current_page, $page_size)
            ->select();

        //获取总条数
        $total = Db::name('customer')->where('customer_company_id', $companyId)->count();

         // 组装结果数据
         $result = [];

         foreach ($customerInfo as $customery)
         {
             $result[] =[
                 'customer_id' => $customery['customer_id'],
                 'customer_name' => $customery['customer_name'],
                 'customer_phone' => $customery['customer_phone'],
                 'customer_address' => $customery['customer_address'],
                 'customer_remark' => $customery['customer_remark'],
                 'customer_creationtime'=> $customery['customer_creationtime'],
                 'customer_updatime'=> $customery['customer_updatime'],
                 'customer_birthday'=> $customery['customer_birthday'],
                 'customer_email'=> $customery['customer_email'],
                 'customer_qq'=> $customery['customer_qq'],
                 'customer_sex' => ($customery['customer_sex'] == 1) ? '男' : (($customery['customer_sex'] == 3) ? '未知' : '女'),
                 'customer_switch' => $customery['customer_switch']

             ];


         }


        if ($customerInfo) {
            return json([
                'code' => 200,
                'msg' => '获取成功',
                'data' => [
                    'data' => $result,
                    'total' => $total,
                ]
            ]);
        } else {
            return json([
                'code' => 400,
                'msg' => '获取失败',
            ]);
        }





    }

    //修改客户信息,要考虑开启事务和捕获异常
    public function Update_CustomerInfo()
    {
        $customer_id = Request::param('id');
        $data=Request::param('data');

        try {
            Db::startTrans();
            Db::name('customer')->where('customer_id', $customer_id)->update($data);
            Db::commit();
            return json([
                'code' => 200,
                'msg' => '修改成功',
            ]);
        } catch (Exception $e) {

            Db::rollback();
            return json([
                'code' => 400,
                'msg' => '修改失败',
            ]);
        }

    }

    //新增客户信息,要考虑开启事务和捕获异常
    public function Insert_CustomerInfo()
    {
        $userId = $this->request->auth['data']->user->user_id;
        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        $data=Request::param('data');

        $data['customer_company_id'] = $companyId;
        $data['customer_switch'] = 1;

        try {
            Db::startTrans();
            Db::name('customer')->insert($data);
            Db::commit();
            return json([
                'code' => 200,
                'msg' => '新增成功',
            ]);
        } catch (Exception $e) {
            Db::rollback();
            return json([
                'code' => 400,
                'msg' => '新增失败',
            ]);
        }

    }

    //删除客户信息,要考虑开启事务和捕获异常
    public function Delete_CustomerInfo()
    {
        $customer_id = Request::param('id');

        try {
            Db::startTrans();
            Db::name('customer')->where('customer_id', $customer_id)->delete($customer_id);
            Db::commit();
            return json([
                'code' => 200,
                'msg' => '删除成功',
            ]);
        } catch (Exception $e) {

            Db::rollback();
            if($e->getCode()==10501)
            {
                return json(['code' => 500, 'msg' => '该客户下已存在单据，无法删除']);
            }
            return json([
                'code' => 400,
                'msg' => '删除失败',
            ]);
        }

    }

}


